package org.zoffy.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.*;
import org.zoffy.thread.AssistantThreadPoolUtil;

import java.util.concurrent.Future;

@RestController
@Slf4j
@RequestMapping("/assistant")
public class AssistantController {
    private final ChatClient chatClient;
    public AssistantController(ChatClient.Builder chatClientBuilder) {
        this.chatClient = chatClientBuilder.build();
    }

    @PostMapping("/chat")
    public String assistant(@RequestBody String userInput){
        try {
            Future<String> future = AssistantThreadPoolUtil.submit(() -> this.chatClient.prompt().user(userInput).call().content());
            return future.get();
        }catch (Exception e){
            log.error("assistant error: " + e.getMessage());
            throw new RuntimeException("assistant error",e);
        }

    }
}
